//
//  PLDirectory.h
//  Plankton
//
//  Created by Marc Schlichte on 2008-07-13.
//  Copyright 2008 Framework Labs. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@class PLPlatform, PLPattern;
@class PLMulticastRequestReplyInteraction;

@protocol PLDirectoryDelegate;

/*!
    @class
    @abstract    A simple Directory Service to look up registered action-ids.
    @discussion  This directory service associates action-ids with service-names
                which can be looked up afterwards. Instances of this class can be
                given the action-ids of peer directory services which are then taken
                into account when doing the lookup.
                This class is primarily meant to be a demonstrator how to use the
                API of the plankton platform and not meant to be THE directory service
                implementation of plankton.
                
*/
@interface PLDirectory : NSObject 
{
    PLPlatform              *platform;
    NSString                *dirAid;
    id<PLDirectoryDelegate> delegate;
    NSMutableDictionary     *serviceToAidsDict;
    NSMutableSet            *peers;
    PLPattern               *queryPattern;
    
    BOOL                    isSearching;
    NSMutableSet            *searchResult;
    NSMutableSet            *outstandingPeers;
    NSString                *serviceToSearch;
    
    PLMulticastRequestReplyInteraction *searchInteraction;
    PLMulticastRequestReplyInteraction *searchReplyInteraction;
    
    BOOL                    discoverable;
    BOOL                    autoDiscover;
    NSNetService            *ownService;
    NSNetServiceBrowser     *serviceBrowser;
    
    BOOL                    receivePushUpdates;
    NSMutableSet            *peersToPush;
    PLPattern               *pushRegistrationPattern;
    PLPattern               *pushPattern;
}

- (id)initWithPlatform:(PLPlatform *)platform;

@property(assign) id<PLDirectoryDelegate> delegate;

@property(readonly) NSString *aid;

- (void)registerAid:(NSString *)aid forService:(NSString *)service;
- (void)unregisterAid:(NSString *)aid;

- (BOOL)searchAidsForService:(NSString *)service;
- (BOOL)searchServices;

- (void)addPeer:(NSString *)aid;
- (void)removePeer:(NSString *)aid;

@property BOOL discoverable;
@property BOOL autoDiscover;

@property BOOL receivePushUpdates;

@end

@protocol PLDirectoryDelegate <NSObject>

- (void)directory:(PLDirectory *)directory foundAid:(NSString *)aid forService:(NSString *)service;
- (void)directory:(PLDirectory *)directory searchDoneForService:(NSString *)service;

- (void)directory:(PLDirectory *)directory foundServices:(NSSet *)services;

@optional
- (void)directoryChangedPeerSet:(PLDirectory *)directory;

- (void)directory:(PLDirectory *)directory addedAid:(NSString *)aid forService:(NSString *)service;
- (void)directory:(PLDirectory *)directory removedAid:(NSString *)aid forService:(NSString *)service;

@end
